package com.woniuxy.controller;

import com.woniuxy.entity.*;
import com.woniuxy.service.CampService;
import com.woniuxy.service.FoodService;
import com.woniuxy.util.Result;
import com.woniuxy.vo.CampVO;
import com.woniuxy.vo.FoodVO;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Hgx
 * @ClassName FoodController
 * @create 2020-05-23 18:12
 */
@RestController
@CrossOrigin
public class FoodController {
    @Resource
    private CampService campServiceImp;

    @Resource
    private FoodService foodServiceImp;

    /**
     *按照城市的id查询营地
     * @param cityId
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectCampByCityId")
    public Result selectCampByCityId(Integer cityId) throws Exception {
        List<Camp> camps = campServiceImp.selectCampByCityId(cityId);
        return new Result("200", "success", null, camps);
    }

    /**
     * 动态的查询该地点的所选营地的菜品  按照订餐量查询
     * 查询该营地所有商品,含升序降序
     * sort 只能是asc 或 desc
     * asc升序
     * desc降序
     * pageIndex当前页
     * 每页默认为6条
     * @param foodName
     * @param campId
     * @param sort
     * @param pageIndex
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectAllFood")
    public Result selectAllFood(String foodName,Integer campId,String sort ,Integer pageIndex ) throws Exception {
        Map map=new HashMap();
        map.put("foodName",foodName);
        map.put("campId",campId);
        map.put("price",sort);
        map.put("pageIndex",pageIndex);
        List<Food> foods = foodServiceImp.selectByFoodNameAndCampId(map);
        return new Result("200","success",foods,foods);
    }
    /**
     * 查询美食商品详情需要foodId
     * @param foodId
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectFoodByFoodId")
    public Result selectFoodByFoodId(Integer foodId) throws Exception{
        Food foods = foodServiceImp.selectFoodByFoodId(foodId);
        return new Result("200","success",foods,null);
    }

    /**
     * 查询该proId的所有评论
     * @param foodId
     * @param pageIndex
     * @return
     */
    @RequestMapping("/selectAllFoodCommentByFoodId")
    public Result selectAllFoodCommentByFoodId(Integer foodId,int pageIndex) throws Exception {
        HashMap map = new HashMap();
        map.put("foodId",foodId);
        map.put("pageIndex",pageIndex);
        List<FoodComment> list = foodServiceImp.selectAllFoodCommentByFoodId(map);
        return new Result("200","success",null,list);
    }

    /**
     *  查询美食订单
     * @param userId
     * @return
     */
    @RequestMapping("/selectAllFoodOrderByUserId")
    public Result selectAllFoodOrderByUserId(Integer userId,Integer status) throws Exception {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userId",userId);
        map.put("status",status);
        List<FoodVO> list = foodServiceImp.selectAllFoodOrderByUserId(map);
        return new Result("200","success",null,list);
    }

    /**
     * 根据营地id查询营地正在使用的车
     * @param campId
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectAllCarByCampId")
    public Result selectAllCarByCampId(Integer campId) throws Exception {
        List<Car> list = foodServiceImp.selectAllCarByCampId(campId);
        return new Result("200","success",null,list);
    }

    /**
     * 新增美食的订单
     * @param foodId
     * @param number
     * @param price
     * @param total
     * @param userId
     * @param campId
     * @param carId
     * @return
     * @throws Exception
     */
    @RequestMapping("/insertFoodOrder")
    public Result insertFoodOrder(Integer foodId,Integer number,Double price,Double total ,Integer userId,Integer campId ,Integer carId) throws Exception {
        FoodOrder foodOrder = new FoodOrder();
        foodOrder.setCampId(campId);
        foodOrder.setCarId(carId);
        foodOrder.setFoodId(foodId);
        foodOrder.setNumber(number);
        // 获取一个Date对象
        Date date = new Date();
        // 将日期时间转换为数据库中的timestamp类型
        Timestamp timeStamp = new Timestamp(date.getTime());
        foodOrder.setOrderTime(timeStamp);
        foodOrder.setPrice(price);
        foodOrder.setStatus(1);
        foodOrder.setTotal(total);
        foodOrder.setUserId(userId);
        int res = foodServiceImp.insertFoodOrder(foodOrder);



        if (res==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }

    /**
     * 查询该商品是否收藏
     * @param foodId
     * @param userId
     * @return
     */
    @RequestMapping("/judgeUserFoodCollection")
    public Result judgeUserFoodCollection(int foodId,int userId){
        System.out.println("foodId="+foodId);
        System.out.println("userId="+userId);
       FoodCollection collection = foodServiceImp.judgeUserFoodCollection(foodId, userId);
        return new Result("200","success",collection,null);
    }

    /**
     * 添加收藏
     * @param foodCollection
     * @return
     */
    @RequestMapping("/addFoodCollection")
    public Result addFoodCollection(FoodCollection foodCollection){
        int i = foodServiceImp.insertFoodCollection(foodCollection);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }

    /**
     * 取消收藏
     * @param foodId
     * @param userId
     * @return
     */
    @RequestMapping("/deleteFoodCollection")
    public Result deleteFoodCollection(int foodId,int userId){
        int i = foodServiceImp.deleteCollection(foodId, userId);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }


    /**
     * 查询该用户美食所有收藏
     * @param userId
     * @return
     */
    @RequestMapping("/selectAllFoodCollection")
    public Result selectAllFoodCollection(int userId){
//        System.out.println("userId="+userId);
//        System.out.println("userId="+userId);
//        int i = Integer.parseInt(userId);
        List<FoodCollection> list = foodServiceImp.selectAllCollection(userId);
        return new Result("200","success",null,list);
    }

    /**
     * 根据id删除收藏
     * @param id
     * @return
     */
    @RequestMapping("/deleteFoodCollectionById")
    public Result deleteFoodCollectionById(int id){
        int i = foodServiceImp.deleteCollectionById(id);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }

    /**
     * 用户支付完成之后的评论美食或者从订单中查询送达的美食进行评论
     * @param foodId
     * @param userId
     * @param comment
     * @return
     */
    @RequestMapping("/insertFoodComment")
    public Result insertFoodComment(Integer foodId,Integer userId,String comment){
        int i = foodServiceImp.insertFoodComment(foodId,userId,comment);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }
    /**
     * 用户支付时候 的密码验证
     */
    @RequestMapping("/passwordValidation")
    public Result passwordValidation(Integer userId ,String password){
        User i = foodServiceImp.selectUserInfoByuserId(userId);
        if (password==i.getPassword()){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }

    /**
     *用户取消美食订单的
     */
    @RequestMapping("/deleteFoodOrder")
    public Result deleteFoodOrder(Integer foodOrderId){
        int res = foodServiceImp.deleteFoodOrder(foodOrderId);
        if (res==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }
    /**
     *查询用户是否对美食评论过
     */
    @RequestMapping("/selectIsOrYesFoodComment")
    public Result selectIsOrYesFoodComment(Integer foodOrderId,Integer foodId,Integer userId){
        int res = foodServiceImp.selectIsOrYesFoodComment(foodOrderId,foodId,userId);
        System.out.println(res);
        if (res==0){
            return new Result("200","success",null,null);

        }else{
            return new Result("400","defeat",null,null);
        }
    }

}
